package models

import (
	"192.168.1.75/go-pkg/helper"
	"gorm.io/gorm"
)

type UpgradeTerminals struct {
	ID                      string `json:"id" gorm:"column:id;primaryKey;type:int;size:64;comment:主键"`
	ScheduleID              string `json:"schedule_id" gorm:"column:schedule_id;type:int;size:64;index:idx_schedule,unique,priority:1;comment:关联的计划id"`
	RobotID                 string `json:"robot_id" gorm:"column:robot_id;type:string;size:40;index:idx_schedule,unique,priority:3;index:idx_robotID_terminalType,priority:1;comment:设备id"`
	TerminalType            string `json:"terminal_type" gorm:"column:terminal_type;type:string;size:64;index:idx_schedule,unique,priority:2;index:idx_robotID_terminalType,priority:2;comment:终端类型"`
	ForceUpgrade            int64  `json:"force_upgrade" gorm:"column:force_upgrade;type:int;size:8;default:0;comment:强制升级"`
	UpgradeStatus           int    `json:"upgrade_status" gorm:"column:upgrade_status;type:int;size:8;comment:升级状态,0-无动作，1-等待下载,2-暂不升级，3-下载中，4-下载完成，5-升级成功,6-升级失败,7-取消升级,8-升级中"`
	UpgradeDownloadProgress int    `json:"upgrade_download_progress" gorm:"column:upgrade_download_progress;type:int;size:8;default:0;comment:升级进度"`
	UpgradeVersion          string `json:"upgrade_version" gorm:"column:upgrade_version;type:string;size:40;comment:升级的版本"`
	CreatedAt               int64  `json:"created_at" gorm:"column:created_at;autoCreateTime;type:int;size:64;comment:创建时间"`
	UpdatedAt               int64  `json:"updated_at" gorm:"column:updated_at;autoUpdateTime:false;type:int;size:64;comment:更新时间"`
}

func (UpgradeTerminals) TableName() string {
	return "upgrade_terminals"
}

// BeforeCreate 自增id
func (u *UpgradeTerminals) BeforeCreate(tx *gorm.DB) (err error) {
	if u.ID != "" {
		return nil
	}
	// 用户id为空时，自动添加用户id
	uuid, err := helper.UUID{}.String()
	if err != nil {
		return err
	}
	u.ID = uuid
	return nil
}

// 模式自动注册
func init() {
	Models = append(Models, &UpgradeTerminals{})
}
